<?php

namespace app\store\controller;

use app\admin\common\Base;
use think\Db;

use app\store\model\Template as Model;
use app\store\model\TemplateDetails as ModelDetails;

use app\store\model\MakeOrder as ModelMakeOrder;
use app\store\model\MakeOrderDetails as ModelMakeOrderDetails;

// 拆卸单
class Disassemble extends Base
{

    public function add()
    {
        $data = $this->request->param();
        $sn = osn('ZZD', 5);

        $store = Db::name('store')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('status', 0)->order('sort desc,id desc')->select();

        $this->assign('store', $store);
        $this->assign('sn', $sn);
        return $this->fetch();
    }

    public function edit()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('make_order')->where('id', $data['id'])->where('type', 0)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $count = Db::name('make_order')->where('id', $data['id'])->where('type', 0)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->count();

            foreach ($list as $key => $value) {
                $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
                $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
                $list[$key]['goodsname'] = $goods['sn'] . ' ' . $goods['name'] . '_' . $goods['model'];
                $list[$key]['dw'] = Db::name('unit')->where('id', $value['unit_id'])->value('name');
                $details = Db::name('make_order_details')->where('pid', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
                $list[$key]['by'] = Db::name('auth_user')->where('id', $value['by'])->value('name');
                $list[$key]['check_user'] = $value['check_user'] != 0 ? Db::name('auth_user')->where('id', $value['check_user'])->value('name') : '';
                $list[$key]['check_time'] = $value['check_time'] == 0 ? '' : date('Y-m-d', $value['check_time']);
                $list[$key]['store_name'] = Db::name('store')->where('id', $value['store_id'])->value('name');
                $list[$key]['use_ck'] = get_stock($value['store_id'], $value['goods_id']);
                // halt($list);
                foreach ($details as $k => $v) {
                    $details_goods = Db::name('goods')->where('id', $v['goods_id'])->find();
                    $details[$k]['goodsname'] = $details_goods['sn'] . ' ' . $details_goods['name'] . '_' . $details_goods['model'];
                    $details[$k]['unit_name'] = Db::name('unit')->where('id', $v['unit_id'])->value('name');
                    $details[$k]['dwcb'] = 0;
                    $details[$k]['cb'] = 0;
                    $details[$k]['store_name'] = Db::name('store')->where('id', $v['store_id'])->value('name');
                    $details[$k]['use_ck'] = get_stock($v['store_id'], $v['goods_id']);
                }
                $list[$key]['son'] = $details;
            }

            // halt($list);
            return $this->grid_data($list, $count, $page, $limit);
        }

        $info = Db::name('make_order')->where('id', $data['id'])->find();
        $info['addtime'] = date('Y-m-d', $info['addtime']);
        $store = Db::name('store')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('status', 0)->order('sort desc,id desc')->select();
        $this->assign('store', $store);
        $this->assign('info', $info);
        $this->assign('id', $data['id']);
        return $this->fetch();
    }

    public function save()
    {
        $data = $this->request->param();
        $mark_order = [
            'addtime' => strtotime($data['addtime']),
            'money' => $data['money'],
            'sn' => $data['sn'],
            'by' => $this->Userinfo['id'],
            'enterprise_id' => $this->EnterpriseId
        ];

        $mark_order = array_merge($mark_order, $data['fixedGrid'][0]);
        // halt($mark_order);
        $model = new ModelMakeOrder();
        Db::startTrans();
        $res = $model->allowField(true)->save($mark_order);
        if (!$res) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }

        foreach ($data['grid'] as $key => $value) {
            $data['grid'][$key]['pid'] = $model->id;
            $data['grid'][$key]['enterprise_id'] = $this->EnterpriseId;
            unset($data['grid'][$key]['id']);
        }

        $modelDetails = new ModelMakeOrderDetails();
        $result = $modelDetails->allowField(true)->saveAll($data['grid']);
        if (!$result) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }

        Db::commit();
        return json(['status' => 1, 'msg' => '添加成功']);
    }

    public function update()
    {
        $data = $this->request->param();
        $mark_order = [
            'addtime' => strtotime($data['addtime']),
            'money' => $data['money'],
            'sn' => $data['sn'],
            'by' => $this->Userinfo['id'],
            'enterprise_id' => $this->EnterpriseId
        ];

        $mark_order = array_merge($mark_order, $data['fixedGrid'][0]);
        // halt($mark_order);
        $model = new ModelMakeOrder();
        Db::startTrans();
        $res = $model->allowField(true)->save($mark_order, ['id' => $data['id']]);
        if (!$res) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }

        $res1 = Db::name('make_order_details')->where('pid', $data['id'])->update(['is_del' => 1]);
        if (!$res1) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }

        foreach ($data['grid'] as $key => $value) {
            $data['grid'][$key]['pid'] = $data['id'];
            $data['grid'][$key]['enterprise_id'] = $this->EnterpriseId;
            unset($data['grid'][$key]['id']);
        }

        $modelDetails = new ModelMakeOrderDetails();
        $result = $modelDetails->allowField(true)->saveAll($data['grid']);
        if (!$result) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }

        Db::commit();
        return json(['status' => 1, 'msg' => '更新成功']);
    }

    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('make_order')->where('type', 0)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $count = Db::name('make_order')->where('type', 0)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->count();
            // halt($list);
            foreach ($list as $key => $value) {
                $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
                $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
                $list[$key]['goods_name'] = $goods['sn'] . ' ' . $goods['name'] . '_' . $goods['model'];
                $list[$key]['unit_name'] = Db::name('unit')->where('id', $value['unit_id'])->value('name');
                $details = Db::name('make_order_details')->where('pid', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
                $list[$key]['by'] = Db::name('auth_user')->where('id', $value['by'])->value('name');
                $list[$key]['check_user'] = $value['check_user'] != 0 ? Db::name('auth_user')->where('id', $value['check_user'])->value('name') : '';
                $list[$key]['check_time'] = $value['check_time'] == 0 ? '' : date('Y-m-d', $value['check_time']);
                foreach ($details as $k => $v) {
                    $details_goods = Db::name('goods')->where('id', $v['goods_id'])->find();
                    $details[$k]['goods_name'] = $details_goods['sn'] . ' ' . $details_goods['name'] . '_' . $details_goods['model'];
                    $details[$k]['unit_name'] = Db::name('unit')->where('id', $v['unit_id'])->value('name');
                    $details[$k]['dwcb'] = 0;
                    $details[$k]['cb'] = 0;
                }
                $list[$key]['son'] = $details;
            }

            return $this->grid_data($list, $count, $page, $limit);
        }

        return $this->fetch();
    }

    public function template()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('template')->where('type', 1)->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            foreach ($list as $key => $value) {
                $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
                $list[$key]['templateName'] = $goods['sn'] . ' ' . $goods['name'] . '_' . $goods['model'];
                $details = Db::name('template_details')->where('pid', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
                foreach ($details as $k => $v) {
                    $sonGoods = Db::name('goods')->where('id', $v['goods_id'])->find();
                    $list[$key]['sonName'][$k] = $sonGoods['sn'] . ' ' . $sonGoods['name'] . '_' . $sonGoods['model'];
                    $list[$key]['sonNum'][$k] = $v['number'];
                }
            }
            // halt($list);
            return $this->grid_data($list, count($list), $page, $limit);
        }
        return $this->fetch();
    }

    // 保存模板
    public function saveTemplate()
    {
        $data = $this->request->param();
        // $template = $data['data'][0];
        $template = [
            'name' => $data['name'],
            'addtime' => time(),
            'enterprise_id' => $this->EnterpriseId,
            'type' => $data['type']
        ];
        $template = array_merge($template, $data['data'][0]);

        $model = new Model();
        Db::startTrans();
        $res = $model->allowField(true)->save($template);
        if (!$res) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }
        foreach ($data['sonData'] as $key => $value) {
            $data['sonData'][$key]['pid'] = $model->id;
            $data['sonData'][$key]['enterprise_id'] = $this->EnterpriseId;
            unset($data['sonData'][$key]['id']);
        }

        $modelDetails = new ModelDetails();
        $result = $modelDetails->allowField(true)->saveAll($data['sonData']);
        if (!$result) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败']);
        }

        Db::commit();
        return json(['status' => 1, 'msg' => '添加成功']);
    }
}
